package leetcode.linkedList.p203;

/**
 * 
 * @Title: Solution203.java 
 * @Package leetcode 
 * @Description: 问题203：移除链表元素，使用递归方式完成
 * @author CandyWall   
 * @date 2021年1月4日 上午12:27:20 
 * @version V1.0
 */
/**
 * Definition for singly-linked list. public class ListNode { int val; ListNode
 * next; ListNode(int x) { val = x; } }
 */
public class Solution203_v5_recursiveImpl {
	public ListNode removeElements(ListNode head, int val) {
        //如果表头为空
        if(head == null) {
            return head;
        }
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next: head;
	}

    public static void main(String[] args) {
		ListNode node = new ListNode(1);
		ListNode p = node;
		p.next = new ListNode(2);
		p = p.next;
		p.next = new ListNode(6);
		p = p.next;
		p.next = new ListNode(3);
		p = p.next;
		p.next = new ListNode(4);
		p = p.next;
		p.next = new ListNode(5);
		p = p.next;
		p.next = new ListNode(6);
		p = p.next;
		
		for(p = node; p != null; p = p.next) {
			System.out.print(p.val + "->");
		}
		System.out.println();
		node = new Solution203_v5_recursiveImpl().removeElements(node, 6);
		for(p = node; p != null; p = p.next) {
			System.out.print(p.val + "->");
		}
		/*ListNode p = node;
		for(p = node; p != null;p = p.next) {
			System.out.print(p.val + "->");
		}
		System.out.println();
		node = new Solution203_v1().removeElements(node, 1);
		for(p = node; p != null;p = p.next) {
			System.out.print(p.val + "->");
		}*/
	}
}


